package top.yvyan.guettable.adapter;

import static java.lang.Double.parseDouble;
import static java.lang.Math.max;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhuangfei.timetable.model.Schedule;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import top.yvyan.guettable.R;
import top.yvyan.guettable.activity.DetailActivity;
import top.yvyan.guettable.bean.CourseBean;
import top.yvyan.guettable.bean.ExamBean;
import top.yvyan.guettable.bean.GradesBean;
import top.yvyan.guettable.data.ScheduleData;
import top.yvyan.guettable.moreFun.GradesActivity;
import top.yvyan.guettable.util.AppUtil;
import top.yvyan.guettable.util.TimeUtil;
import top.yvyan.guettable.widget.WidgetUtil;

public class GradesAdapter extends BaseExpandableListAdapter {
    private final GradesBean gradesBean;
    private final GradesActivity activity;
    private final ExpandableListView view;


    public GradesAdapter(GradesActivity activity, ExpandableListView view, GradesBean gradesBean) {
        this.activity = activity;
        this.gradesBean = gradesBean;
        this.view = view;
        view.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
            gradesBean.switchCustomRule(groupPosition,childPosition-1);
            notifyDataSetChanged();
            return true;
        });
    }

    @Override
    public int getGroupCount() {
        return gradesBean.yearMap.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String periodName = gradesBean.years.get(groupPosition);
        List<GradesBean.Score> scoreList = gradesBean.yearMap.get(periodName);
        if (scoreList == null) {
            return 0;
        }
        return scoreList.size() + 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        String periodName = gradesBean.years.get(groupPosition);
        List<GradesBean.Score> scores = gradesBean.yearMap.get(periodName);
        return scores;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String periodName = gradesBean.years.get(groupPosition);
        List<GradesBean.Score> scores = gradesBean.yearMap.get(periodName);
        return scores.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view;
        GradesViewHolder holder;
        if (convertView == null) {
            view = View.inflate(this.activity, R.layout.grade_cardview, null);
            holder = new GradesViewHolder(view);
        } else {
            view = convertView;
            holder = new GradesViewHolder(convertView);
        }
        holder.itemView.setBackgroundColor(Color.parseColor("#C0FFFFFF"));
        String periodName = gradesBean.years.get(groupPosition);
        List<GradesBean.Score> scores = gradesBean.yearMap.get(periodName);
        DecimalFormat format = new DecimalFormat("0.00");
        Double grade = GradesBean.calcGrade(scores);
        holder.grade.setText(format.format(grade));
        holder.period.setText(periodName + "学年");
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        GradeDetailViewHolder holder;
        if (convertView == null) {
            view = View.inflate(this.activity, R.layout.grade_detail, null);
            holder = new GradeDetailViewHolder(view);
        } else {
            view = convertView;
            holder = new GradeDetailViewHolder(convertView);
        }
        String periodName = gradesBean.years.get(groupPosition);
        List<GradesBean.Score> scores = gradesBean.yearMap.get(periodName);
        double creditSum = GradesBean.calcCreditSum(scores);
        if (childPosition == 0) {
            holder.CourseName.setText("计入学分绩总学分");
            holder.CourseSemester.setText(periodName + "学年");
            holder.Credit.setText(String.format("%.02f 学分", creditSum));
            holder.Calc.setText(this.activity.getResources().getText(R.string.credit_calc_hint));
            holder.itemView.setBackgroundColor(Color.parseColor("#80FFFFFF"));
            return view;
        } else {
            GradesBean.Score score = scores.get(childPosition - 1);
            if (score.consider) {
                holder.itemView.setBackgroundColor(Color.parseColor("#80FFFFFF"));
                switch (score.gaGrade) {
                    case "优":
                    case "良":
                    case "中":
                    case "合格":
                    case "及格":
                    case "不合格":
                    case "不及格":
                        holder.Calc.setText(String.format("%s(%d) × %.3f = %.2f", score.gaGrade, (int) score.score, score.credits / creditSum, score.score * (score.credits / creditSum)));
                        break;
                    default:
                        holder.Calc.setText(String.format("%d × %.3f = %.2f", (int) score.score, score.credits / creditSum, score.score * (score.credits / creditSum)));
                        break;
                }
            } else {
                holder.Calc.setText(String.format("不计入学分绩 (%s)",score.gaGrade));
                holder.itemView.setBackgroundColor(Color.parseColor("#A0D0D0D0"));
            }
            holder.CourseName.setText(score.courseName);
            holder.CourseSemester.setText(score.semesterName);
            holder.Credit.setText(String.format("%.02f 学分", score.credits));
            holder.CourseName.setText(score.courseName);
            return view;
        }
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return childPosition != 0;
    }


    public static class GradesViewHolder {
        public View itemView;
        public TextView period, grade;

        public GradesViewHolder(@NonNull View itemView) {
            this.period = itemView.findViewById(R.id.grades_period);
            this.grade = itemView.findViewById(R.id.grades_main);
            this.itemView = itemView;
        }
    }

    public static class GradeDetailViewHolder {
        View itemView;
        TextView CourseName, CourseSemester, Credit, Calc;

        public GradeDetailViewHolder(@NonNull View itemView) {
            this.itemView = itemView;
            this.CourseName = itemView.findViewById(R.id.course_name);
            this.CourseSemester = itemView.findViewById(R.id.course_semester);
            this.Credit = itemView.findViewById(R.id.course_credit);
            this.Calc = itemView.findViewById(R.id.course_calc);
        }
    }
}
